﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeJC.DAL.Model;
using DeJC.DAL.Repository;

namespace DeJC.BLL {
    public class UnitOfWork:IDisposable {
        private TheListEntities _context;
        public UnitOfWork() {
            _context=new TheListEntities();
        }
        #region private variable

        private CityRepository _cityRepository;
        private CountryRepository _countryRepository;
        private GeneralMiscTypeRepository _generalMiscTypeRepository;
        private ProductRepository _productRepository;
        private UserRepository _userRepository;
        private UserAddressRepository _userAddressRepository;
        private UserContactRepository _userContactRepository;
        private UserListRepository _userListRepository;
        private UserLogRepository _userLogRepository;
        private UserListProductRepository _userListProductRepository;
        #endregion

        #region Public Properties
        public CityRepository CityRepository {
            get {

                if(this._cityRepository==null) {
                    this._cityRepository=new CityRepository(_context);
                }
                return _cityRepository;
            }
        }
        public CountryRepository CountryRepository {
            get {

                if(this._countryRepository==null) {
                    this._countryRepository=new CountryRepository(_context);
                }
                return _countryRepository;
            }
        }
        public GeneralMiscTypeRepository GeneralMiscTypeRepository {
            get {

                if(this._generalMiscTypeRepository==null) {
                    this._generalMiscTypeRepository=new GeneralMiscTypeRepository(_context);
                }
                return _generalMiscTypeRepository;
            }
        }
        public ProductRepository ProductRepository {
            get {

                if(this._productRepository==null) {
                    this._productRepository=new ProductRepository(_context);
                }
                return _productRepository;
            }
        }
        public UserRepository UserRepository {
            get {

                if(this._userRepository==null) {
                    this._userRepository=new UserRepository(_context);
                }
                return _userRepository;
            }
        }
        public UserAddressRepository UserAddressRepository {
            get {

                if(this._userAddressRepository==null) {
                    this._userAddressRepository=new UserAddressRepository(_context);
                }
                return _userAddressRepository;
            }
        }
        public UserContactRepository UserContactRepository {
            get {

                if(this._userContactRepository==null) {
                    this._userContactRepository=new UserContactRepository(_context);
                }
                return _userContactRepository;
            }
        }
        public UserListRepository UserListRepository {
            get {

                if(this._userListRepository==null) {
                    this._userListRepository=new UserListRepository(_context);
                }
                return _userListRepository;
            }
        }
        public UserLogRepository UserLogRepository {
            get {

                if(this._userLogRepository==null) {
                    this._userLogRepository=new UserLogRepository(_context);
                }
                return _userLogRepository;
            }
        }

        public UserListProductRepository UserListProductRepository
        {
            get
            {
                if(this._userListProductRepository==null)
                {
                    this._userListProductRepository = new UserListProductRepository(_context);
                }
                return _userListProductRepository;
            }
        }
        #endregion
        public void Save() {
            _context.SaveChanges();
        }

        private bool _disposed=false;

        protected virtual void Dispose(bool disposing) {
            if(!this._disposed) {
                if(disposing) {
                    _context.Dispose();
                }
            }
            this._disposed=true;
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
